package Entity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Control.MySQLController;

public class Shop_Customization {
	
	private String frontCustUrl, backCustUrl, custId, orderId;
	private Shop_Product product;
	private int quantity;
	private Shop_CustomizationStatusCode custStatusCode;
	
	public Shop_Customization(){}
	
	//Constructor for creating customization object
	public Shop_Customization(String frontCustUrl, String backCustUrl, Shop_Product product, String custId, String orderId, int quantity){
		
		this.setFrontCustUrl(frontCustUrl);
		this.setBackCustUrl(backCustUrl);
		this.setProduct(product);
		this.setOrderId(orderId);
		
		this.custId = custId;
		
		this.quantity = quantity;
		
		this.custStatusCode = new Shop_CustomizationStatusCode("PEN");
		
	}

	//Constructor for retrieving customization
	public Shop_Customization(String custId){
		
		this.custId = custId;
		
	}
	
	public Shop_Product getProduct() {
		return product;
	}

	public void setProduct(Shop_Product product) {
		this.product = product;
	}

	public String getCustId() {
		return custId;
	}

	public void setCustId(String custId) {
		this.custId = custId;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public Shop_CustomizationStatusCode getCustStatusCode() {
		return custStatusCode;
	}

	public String getFrontCustUrl() {
		return frontCustUrl;
	}

	public void setFrontCustUrl(String frontCustUrl) {
		this.frontCustUrl = frontCustUrl;
	}

	public String getBackCustUrl() {
		return backCustUrl;
	}

	public void setBackCustUrl(String backCustUrl) {
		this.backCustUrl = backCustUrl;
	}

	/**
	 * @return the orderId
	 */
	public String getOrderId() {
		return orderId;
	}

	/**
	 * @param orderId the orderId to set
	 */
	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}
	
	public void setCustStatusCode(Shop_CustomizationStatusCode custStatusCode) {
		this.custStatusCode = custStatusCode;
	}
	
	public static ArrayList<Shop_Customization> retrieveProdCustomizations(String prodId, int month){
		ArrayList<Shop_Customization> custs = new ArrayList<Shop_Customization>();
		MySQLController db = new MySQLController();
		ResultSet rs = null;
		db.getConnection();
		String sql = "SELECT * FROM customization ";
		sql += "INNER JOIN order_form ON customization.order_id = order_form.order_id ";
		sql += "WHERE product_id = '" + prodId + "' AND MONTH(order_date) = " + (month+1);
		rs = db.readRequest(sql);
		try {
			while(rs.next()){
				Shop_Customization cust = new Shop_Customization();
				cust.setProduct(new Shop_Product(prodId));
				cust.setFrontCustUrl(rs.getString("front_cust_url"));
				cust.setBackCustUrl(rs.getString("back_cust_url"));
				cust.setQuantity(rs.getInt("quantity"));
				custs.add(cust);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.terminate();
		return custs;
	}

	public boolean createCustomization(){
		boolean success = false;
		
		MySQLController db = new MySQLController();
		db.getConnection();
		String sql = "INSERT INTO ooadpj.customization (product_id, cust_id, order_id, front_cust_url, back_cust_url, quantity, cust_status_code) VALUES ('" + product.getProductId() + "', '" + custId + "', '" + orderId + "', '" + frontCustUrl + "', '" + backCustUrl + "', " + quantity + ", 'PEN')";
		if(db.updateRequest(sql) == 1){
			success = true;
		}
		db.terminate();
		
		return success;
	}

}
